<div class="box box-info">
    <div class="box-header with-border">
        <h3 class="box-title">菜单管理</h3>
    </div>
    <!-- /.box-header -->
    <!-- form start -->
    <form class="form-horizontal">
        <div class="box-body">
            <div class="form-group">
                <label for="nameId" class="col-sm-2 control-label">类型</label>

                <div class="col-sm-10 typeRadio">
                    <label class="radio-inline">
                        <input type="radio" name="typeId" value="1"> 菜单</label>
                    <label class="radio-inline">
                        <input type="radio" name="typeId" value="2"> 按钮</label>
                </div>
            </div>
            <div class="form-group">
                <label for="nameId" class="col-sm-2 control-label">菜单名称</label>

                <div class="col-sm-10">
                    <input type="text" class="form-control" id="nameId" placeholder="名称">
                </div>
            </div>
            <div class="form-group">
                <label for="parentId" class="col-sm-2 control-label">上级菜单</label>

                <div class="col-sm-10">
                    <input type="text" readonly="readonly" class="form-control load-sys-menu" id="parentId" placeholder="上级菜单">
                </div>
            </div>
            <div class="form-group">
                <label for="urlId" class="col-sm-2 control-label">菜单URL</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="urlId" placeholder="菜单URL">
                </div>
            </div>
            <div class="form-group">
                <label for="permissionId" class="col-sm-2 control-label">授权标识</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="permissionId" placeholder="多个用逗号分隔">
                </div>
            </div>
            <div class="form-group">
                <label for="sortId" class="col-sm-2 control-label">排序号</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="sortId" placeholder="排序号">
                </div>
            </div>

        </div>
        <!-- /.box-body -->
        <div class="box-footer">
            <button type="button" class="btn btn-default btn-cancel">Cancel</button>
            <button type="button" class="btn btn-info pull-right btn-add">Save</button>
        </div>
        <!-- /.box-footer -->

        <!-- zTree 对应的div -->
        <div class="layui-layer layui-layer-page layui-layer-molv layer-anim" id="menuLayer" type="page" times="2" showtime="0" contype="object"
             style="z-index:59891016; width: 300px; height: 450px; top: 100px; left: 500px; display:none">
            <div class="layui-layer-title" style="cursor: move;">选择菜单</div>
            <div class="layui-layer-content" style="height: 358px;">
                <div style="padding: 10px;" class="layui-layer-wrap">
                    <ul id="menuTree" class="ztree"></ul>    <!-- 动态加载树 -->
                </div>
            </div>
            <span class="layui-layer-setwin"> <a class="layui-layer-ico layui-layer-close layui-layer-close1 btn-cancel" ></a></span>
            <div class="layui-layer-btn layui-layer-btn-">
                <a class="layui-layer-btn0 btn-confirm">确定</a>
                <a class="layui-layer-btn1 btn-cancel">取消</a>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="/bower_components/ztree/jquery.ztree.all.min.js"></script>
<script type="text/javascript" src="/bower_components/layer/layer.js"></script>
<script type="text/javascript">
    var zTree;
    var  setting={
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootPId: null
            }
        }

    };

    $(function () {
        $("#mainID").on("click",".load-sys-menu",doLoadZtreeMenuNodes);
        $("#menuLayer").on("click",".btn-confirm",doSetSelectNode)
            .on("click",".btn-cancel",doHideTree);
        $(".box-footer").on("click",".btn-add",doSaveOrUpdate)
            .on("click",".btn-cancel",doCancelList);
        var data=$("#mainID").data("rowData");
        if(data){
            doInitEditFormData(data);
        }
    });
    function doCancelList() {
        var url="menu/menu_list";
        $("#mainID").load(url);
    }
    function doInitEditFormData(data) {
        $(".typeRadio input[value='"+data.type+"']").prop("checked",true);
        $("#nameId").val(data.name);
        $("#parentId").val(data.parentName);
        $("#parentId").data("parentId",data.parentId);
        $("#sortId").val(data.sort);
        $("#urlId").val(data.url);
        $("#permissionId").val(data.permission);
    }



    function doSaveOrUpdate() {
        //获取表单数据
        var params=doGetEditFormData();
        var rowData=$("#mainID").data("rowData");
        console.log(params);
        var insertUrl="menu/doSaveObject";
        var updateUrl="menu/doUpdateObject";
        var url=rowData?updateUrl:insertUrl;
        if(rowData){
         params.id=rowData.id;
        }
        $.post(url,params,function (result) {
            if(result.state==1){
                alert("操作成功");
                // doCancel();
                var url="menu/menu_list";
                $("#mainID").load(url);
            }else {
                alert(result.message);
            }
        })
    }

    function doGetEditFormData() {
        var params={
            type: $("form input[name='typeId']:checked").val(),
            name: $("#nameId").val(),
            url: $("#urlId").val(),
            sort: $("#sortId").val(),
            permission: $("#permissionId").val(),
            sortId: $("#sortId").val(),
            parentId: $("#parentId").data("parentId")
        };
        return params;
    }
//加载树结构
    function doLoadZtreeMenuNodes() {
        var url="/menu/doFindZtreeMenuNodes";
        $.getJSON(url,function (result) {
            if (result.state==1){
                //使用init方法进行初始化
                zTree=$.fn.zTree.init(
                    $("#menuTree"),
                    setting,
                    result.data
                );
                //显示出来
                $("#menuLayer").css("display","block");
            }else{
                alert(result.message);
            }
        })
    }
    //隐藏树
    function doHideTree() {
        $("#menuLayer").css("display","none");
    }
    //将选中的节点 设置到input标签中 显示出来
    function doSetSelectNode() {
        //获取选中的对象
        var nodes=zTree.getSelectedNodes();
        if(nodes.length==1){
            var node=nodes[0];
            console.log(node);
            //将内容填充到input标签中
            $("#parentId").data("parentId",node.id);
            $("#parentId").val(node.name);
        }
        doHideTree();
    }
</script>
